package com.lxh.lxhcrm.controller.presale;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxh.lxhcrm.model.pojos.OppoExam;
import com.lxh.lxhcrm.model.pojos.OppoState;
import com.lxh.lxhcrm.model.pojos.Opportunity;
import com.lxh.lxhcrm.model.service.PreSaleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开发团队 ：lxh
 * 开发人员 ：罗晓
 * 开发时间 ：2020/1/9 10:34
 * 类的名称 ：PreSaleControlle
 * 类的描述 ：售前模块销售机会的controller，进行前后端的数据传输控制
 */
@RestController
@RequestMapping("/oppo-sale")
public class PreSaleControlle {
    @Resource
    PreSaleService ps;

    /**
     * 带分页的查询
     * @param currPage
     * @param pageSize
     * @param state
     * @return
     */
    @RequestMapping("/sale-oppo")
    public Map<String,Object> OppoList(Integer currPage, Integer pageSize,Integer state,String search,Integer searchBy,Integer customerId){
        if (currPage == null || currPage == 0){ currPage = 1;}
        if (pageSize == null || pageSize == 0){ pageSize = 8;}
        /* 分页 */
        Page<Object> page = PageHelper.startPage(currPage, pageSize);
        Map<String,Object> resoppo = new HashMap<>();
        List<Opportunity> list = ps.findAllOppo(state,search,searchBy,customerId);
        resoppo.put("data",list);
        resoppo.put("total",page.getTotal());
        return resoppo;
    }

    /**
     * 不带分页的查询
     * @param state
     * @return
     */
    @RequestMapping("/oppoNoPage")
    public List<Opportunity> OppoList(Integer state,String search,Integer searchBy,Integer customerId){
        if (state == null){state = 0;}
        List<Opportunity> list = ps.findAllOppo(state,search,searchBy,customerId);
        return list;
    }

    /**
     * 获取机会状态
     * @return
     */
    @GetMapping("/oppoStates")
    public List<OppoState> getOppoState(){
        return ps.getOppoState();
    }

    /**
     * 查询客户是否已是销售机会状态
     * @param customerId
     * @return
     */
    @RequestMapping("/judgeIsOppo")
    public Integer judgeIsOppo(Integer customerId){
        return ps.judgeIsOppo(customerId);
    }

    /**
     * 通过customer查询此用户的opportunity
     * @param state
     * @param search
     * @param customerId
     * @return
     * 备注： 杨玲 客户视图使用过
     */
    @PostMapping("/findOppoByCus")
    public List<Opportunity> findOppoByCus(Integer state,String search,Integer searchBy,Integer customerId){return ps.findOppoByCus(state,search,searchBy,customerId);}

    /**
     * 添加和修改的方法
     * @param opportunity
     */
    @PostMapping("/saveOppo")
    public void saveOpportunity(@RequestBody Opportunity opportunity){
        ps.saveOpportunity(opportunity);
    }

    /**
     * 根据oppoId来查询OppoExam
     * @param oppoId
     * @return
     */
    @RequestMapping("/findOppoExam")
    public List<OppoExam> findOppoExam(Integer oppoId){
        return ps.findOppoExam(oppoId);
    }

    /**
     * 保存
     * @param oppoExam
     */
    @PostMapping("/saveOExam")
    public void saveOppoExam(@RequestBody OppoExam oppoExam){
        System.out.println(oppoExam.getEmp().getEmpId());
        System.out.println(oppoExam.getOppo().getOppoId());
        oppoExam.setOeId(0);
        ps.saveOppoExam(oppoExam);
    }

    /**
     * 根据empId，oppoStateId，date来查询detail_offer里的报价产品
     * @param empId
     * @param oppoStateId
     * @param date
     * @return
     */
    @RequestMapping("/findNeedPro")
    public List<Map<String,Object>> findNeedPro(Integer empId, Integer oppoStateId, Date date){
        return ps.findNeedPro(empId, oppoStateId,date);
    }

    /**
     * 根据pro_detail_id,来查找有此类商品的销售机会
     * @param currPage
     * @param pageSize
     * @param proDetailId
     * @return
     */
    @RequestMapping("/findDetailPro")
    public Map<String,Object> findDetailPro(Integer currPage, Integer pageSize, Integer proDetailId, Date date){
        if (currPage == null || currPage == 0){ currPage = 1;}
        if (pageSize == null || pageSize == 0){ pageSize = 4;}
        Page<Object> page = PageHelper.startPage(currPage, pageSize);
        Map<String,Object> dep = new HashMap<>();
        List<Map<String,Object>> pro = ps.findDetailPro(proDetailId,date);
        dep.put("data",pro);
        dep.put("total",page.getTotal());
        return dep;
    }

    /**
     * 简单的查询Emp
     * @return
     */
    @GetMapping("/findEasyEmp")
    public List<Map<String,Object>> findEasyEmp(){
        return ps.findEasyEmp();
    }

    /**
     * 查询此用户是否具备审核功能
     * @param empId
     * @return
     */
    @RequestMapping("/findDuty")
    public boolean findDuty(Integer empId){
        return ps.findDuty(empId);
    }

    /**
     * 根据oppoId来灭活
     * @param oppoId
     */
    @RequestMapping("/deleteOppo")
    public void deleteOppo(Integer oppoId){
        System.out.println(oppoId);
        ps.deleteOppo(oppoId);
    }

    /**
     * 单纯的机会查询
     * @return
     */
    @RequestMapping("/selectOppo")
    public List<Map<String,Object>> selectOppo(){
        return ps.selectOppo();
    }
}
